package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //注入Dao
    @Autowired
    private CheckItemDao checkItemDao;
    //新增检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //分页助手插件
        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> checkItems = checkItemDao.selectByCondition(queryString);
        long total = checkItems.getTotal();
        List<CheckItem> rows = checkItems.getResult();
        PageResult pageResult = new PageResult(total, rows);
        return pageResult;
    }
    //删除检查项
    @Override
    public void deleteById(Integer id) {
        //判断检查项是否有外键关联
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count>0){
        //存在外键关联,不允许删除
            new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }
    //编辑检查项
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }
    //回显检查项
    @Override
    public CheckItem findById(Integer id) {
      CheckItem checkItem=checkItemDao.findById(id);
        return checkItem;
    }
    //查询所有检查项信息
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
